package com.liuhengjia.springaiapply.initialize;

import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.QuestionAnswerAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.InMemoryChatMemory;
import org.springframework.ai.moonshot.MoonshotChatModel;
import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 模型初始化类
 */
@Configuration
@RequiredArgsConstructor
public class ModelInit {
    private final MoonshotChatModel moonshotChatModel;
    private final OllamaChatModel ollamaChatModel;
    private final VectorStore vectorStore;

    @Bean
    public ChatClient chatClient(ChatMemory chatMemory) {
        return ChatClient.builder(ollamaChatModel)
//                .defaultSystem("假如你是漫威中的贾维斯。接下来的对话你必须以贾维斯的语气来进行")
                // 负责读取、拼接、传递
                .defaultAdvisors(
                        new MessageChatMemoryAdvisor(chatMemory),
                        new QuestionAnswerAdvisor(vectorStore)
                )
                .build();
    }

    @Bean
    public ChatMemory chatMemory() {
        // 负责存和读：存在内存中
        return new InMemoryChatMemory();
    }
}
